package wumingya.com.studentsystem;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import reflash.ApkEntity;
import reflash.MyAdapter;
import reflash.ReFlashListView;

public class ClassFragment extends Fragment implements ReFlashListView.IReflashListener
{
	static ArrayList<ApkEntity> apk_list;
	private Gson gson;
	private ApkEntity a;
	private ListView mListView;
	private List<Map<String, Object>> oList;
	private View myView;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
//		if (myView != null) {
//            ViewGroup parent = (ViewGroup) myView.getParent();
//            if (parent != null) {
//                parent.removeView(myView);
//            }
//			Log.i("tag","3");
//            return myView;
//        }
		//取得从上一个Activity当中传递过来的Intent对象
		//String url = intent.getStringExtra("url");
		myView = inflater.inflate(R.layout.tab03, container, false);
		request();
//		this.setData();
//		this.showList(apk_list);

		return myView;
	}
	/*
   * 下拉刷新功能
   * */
	static MyAdapter adapter;
	static ReFlashListView listview;


	private void showList(ArrayList<ApkEntity> apk_list) {
		if (adapter == null) {
			listview = (ReFlashListView) myView.findViewById(R.id.listview);
			listview.setInterface(this);
			adapter = new MyAdapter(this.getContext(), apk_list);
			listview.setAdapter(adapter);

			//添加页面转跳的监听
			listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					//获取当前实体类
					Adapter adpter=parent.getAdapter();
					ApkEntity a = (ApkEntity) adpter.getItem(position);
					String a_name = a.getName();
					String a_id = a.getId();
					String a_classname = a.getClass_name();
					Intent intent =new Intent(getActivity(),DetailActivity.class);
					intent.putExtra("name",a_name);
					intent.putExtra("id",a_id);
					intent.putExtra("classname",a_classname);
					startActivity(intent);
				}
			});

		} else {
			adapter.onDateChange(apk_list);
		}
	}

	private void setReflashData() {
		this.request();
//		for (int i = 0; i < 2; i++) {
//			ApkEntity entity = new ApkEntity();
//			entity.setName("刷新数据");
//			entity.setmajor_name("这是一个神奇的应用");
//			entity.setschool_name("50w用户");
//			apk_list.add(0,entity);
//		}
	}
	@Override
	public void onReflash() {
		request();
		// TODO Auto-generated method stub\
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				//获取最新数据
//				setReflashData();
				//通知界面显示
				showList(apk_list);
				//通知listview 刷新数据完毕；
				listview.reflashComplete();
			}
		}, 2000);
	}

//	public static void doDeleteStudent(final View view, final Context context, final int position){
//		deleteRequest("13",view, context, position);
//
//	}
	public static void setAnimation(final View view, final Context context, final int position, String id) {
		deleteRequest(id,view, context, position);
		ObjectAnimator oaY=ObjectAnimator.ofFloat(view,"scaleY", 1f,0f);
		ObjectAnimator oaX=ObjectAnimator.ofFloat(view,"scaleX", 1f,0f);
		AnimatorSet animSet = new AnimatorSet();
		animSet.setDuration(1000);
		animSet.playTogether(oaY,oaX);
		animSet.start();
		//设置动画执行的监听事件
		animSet.addListener(new Animator.AnimatorListener() {
			@Override
			public void onAnimationStart(Animator animation) {

			}

			@Override
			public void onAnimationEnd(Animator animation) {
				apk_list.remove(position);

				adapter.notifyDataSetChanged();
				listview.setAdapter(adapter);
				Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onAnimationCancel(Animator animation) {

			}

			@Override
			public void onAnimationRepeat(Animator animation) {

			}
		});
	}

	public static void deleteRequest(String id, final View view, final Context context, final int position) {
		//步骤4:创建Retrofit对象
		//创建okHttpClient对象
		OkHttpClient mOkHttpClient = new OkHttpClient();
		//创建一个Request
		final Request request = new Request.Builder()
				.url("http://10.0.2.2/stuServer/api/deleteStudent" + "?id=" + id)
				.build();
		//new call
		Call call = mOkHttpClient.newCall(request);
		call.enqueue(new Callback()
		{
			@Override
			public void onFailure(Call call, IOException e) {
				Log.i("kwwl","获取数据失败了");
			}
			@Override
			public void onResponse(Call call, Response response) throws IOException {
				if(response.isSuccessful()){//回调的方法执行在子线程。
					Log.i("kwwl","获取数据成功了");
					//Log.i("kwwl","response.code()=="+response.code());
					String responseData = response.body().string();
					//Log.i(TAG, response.body().string());
//					setAnimation(view,context,position);
//					ClassFragment.this.myView.post(new Runnable() {
//						public void run() {
//							ClassFragment.this.
//						}
//					});
				}
			}
		});
	}


	public void request() {
		//步骤4:创建Retrofit对象
		//创建okHttpClient对象
		OkHttpClient mOkHttpClient = new OkHttpClient();
		//创建一个Request
		final Request request = new Request.Builder()
				.url("http://10.0.2.2/stuServer/api/students")
				.build();
		//new call
		Call call = mOkHttpClient.newCall(request);
		//请求加入调度
		call.enqueue(new Callback()
		{
			@Override
			public void onFailure(Call call, IOException e) {
				Log.i("kwwl","获取数据失败了");
			}
			@Override
			public void onResponse(Call call, Response response) throws IOException {
				if(response.isSuccessful()){//回调的方法执行在子线程。
					Log.i("kwwl","获取数据成功了");
					//Log.i("kwwl","response.code()=="+response.code());
					String responseData = response.body().string();
					//Log.i(TAG, response.body().string());
					parseJSONWithGSON(responseData);

					ClassFragment.this.myView.post(new Runnable() {
						public void run() {
							ClassFragment.this.showList(apk_list);
						}
					});
				}
			}
		});
	}
	private void parseJSONWithGSON(String jsonData) {
		Gson gson = new Gson();
		apk_list = gson.fromJson(jsonData, new TypeToken<ArrayList<ApkEntity>>() {
		}.getType());

	}

}

